/**
 * @file test_basic.c
 * @brief 基本功能测试
 */

#include "action_camera.h"
#include <assert.h>

// 测试消息队列功能
void test_message_queue(void) {
    printf("Testing message queue...\n");
    
    message_queue_t queue;
    assert(message_queue_init(&queue, 10) == 0);
    
    // 测试发送消息
    message_t msg = {
        .type = MSG_CONTROL,
        .timestamp = 123456,
        .source_thread = THREAD_MAIN,
        .dest_thread = THREAD_CAMERA,
        .data_size = 5
    };
    strcpy((char*)msg.data, "test");
    
    assert(message_queue_send(&queue, &msg) == 0);
    
    // 测试接收消息
    message_t received_msg;
    assert(message_queue_receive(&queue, &received_msg, 0) == 0);
    
    assert(received_msg.type == MSG_CONTROL);
    assert(received_msg.timestamp == 123456);
    assert(received_msg.source_thread == THREAD_MAIN);
    assert(received_msg.dest_thread == THREAD_CAMERA);
    assert(received_msg.data_size == 5);
    assert(strcmp((char*)received_msg.data, "test") == 0);
    
    message_queue_destroy(&queue);
    printf("Message queue test passed\n");
}

// 测试帧缓冲池功能
void test_frame_pool(void) {
    printf("Testing frame pool...\n");
    
    frame_pool_t pool;
    assert(frame_pool_init(&pool, 5) == 0);
    
    // 测试获取帧
    shared_frame_t* frame1 = frame_pool_acquire(&pool);
    assert(frame1 != NULL);
    assert(atomic_load(&frame1->ref_count) == 1);
    
    shared_frame_t* frame2 = frame_pool_acquire(&pool);
    assert(frame2 != NULL);
    assert(frame1 != frame2);
    
    // 测试释放帧
    frame_pool_release(&pool, frame1);
    assert(atomic_load(&frame1->ref_count) == 0);
    
    // 测试重新获取帧
    shared_frame_t* frame3 = frame_pool_acquire(&pool);
    assert(frame3 == frame1); // 应该获取到之前释放的帧
    
    frame_pool_release(&pool, frame2);
    frame_pool_release(&pool, frame3);
    
    frame_pool_destroy(&pool);
    printf("Frame pool test passed\n");
}

// 测试时间戳功能
void test_timestamp(void) {
    printf("Testing timestamp...\n");
    
    uint64_t ts1 = get_system_timestamp();
    usleep(10000); // 10ms
    uint64_t ts2 = get_system_timestamp();
    
    assert(ts2 > ts1);
    assert((ts2 - ts1) >= 10000); // 至少10ms
    assert((ts2 - ts1) < 50000);  // 不超过50ms
    
    printf("Timestamp test passed\n");
}

// 测试线程心跳功能
void test_heartbeat(void) {
    printf("Testing heartbeat...\n");
    
    uint64_t ts1 = get_system_timestamp();
    update_thread_heartbeat(THREAD_CAMERA);
    uint64_t ts2 = get_system_timestamp();
    
    assert(g_camera_system.threads[THREAD_CAMERA].last_heartbeat >= ts1);
    assert(g_camera_system.threads[THREAD_CAMERA].last_heartbeat <= ts2);
    
    printf("Heartbeat test passed\n");
}

int main(void) {
    printf("=== Action Camera Basic Tests ===\n");
    
    // 初始化系统
    assert(system_init() == 0);
    
    // 运行各项测试
    test_message_queue();
    test_frame_pool();
    test_timestamp();
    test_heartbeat();
    
    // 清理系统
    system_cleanup();
    
    printf("=== All Basic Tests Passed ===\n");
    return 0;
}